<?php
namespace app\admin\controller;
require '../vendor/autoload.php';

use think\Db;
use think\Controller;
class GoodsSpecification extends Controller{
    public function index(){
        if($_POST){
//            var_dump($_POST);exit;
            $map = array();
            $category_name = $_POST['category_name'];//分类名称
            if($category_name){
                $category_id = Db::table('goods_category')->where(array('name'=>$category_name))->value('id');
                $map['cid']    =   array('eq',$category_id);
                $this->assign('category_name',$category_name);
            }
            $specification_name = $_POST['specification_name'];//规格名
            if($specification_name){
                $map['name']    =   array('like', '%'.(string)$specification_name.'%');
                $this->assign('specification_name',$specification_name);
            }
            $start = $_POST['start'];//时间
            $end = $_POST['end'];
            if($start && $end){
                $map['create_time'] = array('between',strtotime($start).','.strtotime($end));
                $this->assign('start',$start);
                $this->assign('end',$end);
            }elseif($start){
                $map['create_time'] = array('egt',strtotime($start));
                $this->assign('start',$start);
            }elseif($end){
                $map['create_time'] = array('elt',strtotime($end));
                $this->assign('end',$end);
            }
            $status = $_POST['status'];//状态
            if($status){
                if($status==2){
                    $status = 0;
                }
                $map['status'] = array('eq',$status);
                $this->assign('status',$status);
            }

            $re_goods_specification = Db::table('goods_specification')->where($map)->select();
//            var_dump($re_goods_specification);exit;
        }else{
            //查询所有的规格名
            $re_goods_specification = Db::table('goods_specification')->select();
        }
        //查询商品类型
        if(!empty($re_goods_specification)){
            foreach($re_goods_specification as $k=>$v){
                $category = Db::table('goods_category')->where(array('id'=>$v['cid']))->find();
                $re_goods_specification[$k]['category_name'] = $category['name'];
            }
        }
        $this->assign('goods_specification',$re_goods_specification);
        return view('index');
    }
    /*
     * 导出excle
     */
    public function toExcel(){
        $map = array();
        $category_name = $_GET['category_name'];//分类名称
        if($category_name){
            $category_id = Db::table('goods_category')->where(array('name'=>$category_name))->value('id');
            $map['cid']    =   array('eq',$category_id);
            $this->assign('category_name',$category_name);
        }
        $specification_name = $_GET['specification_name'];//规格名
        if($specification_name){
            $map['name']    =   array('like', '%'.(string)$specification_name.'%');
            $this->assign('specification_name',$specification_name);
        }
        $start = $_GET['start'];//时间
        $end = $_GET['end'];
        if($start && $end){
            $map['create_time'] = array('between',strtotime($start).','.strtotime($end));
            $this->assign('start',$start);
            $this->assign('end',$end);
        }elseif($start){
            $map['create_time'] = array('egt',strtotime($start));
            $this->assign('start',$start);
        }elseif($end){
            $map['create_time'] = array('elt',strtotime($end));
            $this->assign('end',$end);
        }
        $status = $_GET['status'];//状态
        if($status){
            if($status==2){
                $status = 0;
            }
            $map['status'] = array('eq',$status);
            $this->assign('status',$status);
        }

        $re_goods_specification = Db::table('goods_specification')->where($map)->select();

        //导出excel
        //import('./vendor/PHPExcel-1.8/Classes/PHPExcel.php');
        vendor('PHPExcel.Classes.PHPExcel');
        $objPHPExcel = new \PHPExcel();
        $fileName = 's';
        $headArr = array(1,2,3,4);
        $data = $re_goods_specification;
        $fileName .= "_" . date("Y_m_d") . ".xls";

        $objPHPExcel = new \PHPExcel();

        $objPHPExcel->getProperties();

        $key = ord("A"); // 设置表头

        foreach ($headArr as $v) {

            $colum = chr($key);

            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);

            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);

            $key += 1;

        }

        $column = 2;

        $objActSheet = $objPHPExcel->getActiveSheet();

        foreach ($data as $key => $rows) { // 行写入

            $span = ord("A");

            foreach ($rows as $keyName => $value) { // 列写入

                $objActSheet->setCellValue(chr($span) . $column, $value);

                $span++;

            }

            $column++;

        }

        $fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表

        $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表

        header('Content-Type: application/vnd.ms-excel');

        header("Content-Disposition: attachment;filename='$fileName'");

        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $objWriter->save('php://output'); // 文件通过浏览器下载

        exit();
    }
    public function edit(){
        if($_POST){//编辑
            $id = input("id");
            $parent_id = $_POST['cid'];
            $name = $_POST['name'];
            $status = $_POST['status'];
            //如果是父级就不能修改
            $is_parent = Db::table('goods_category')->where(array('parent_id'=>$parent_id))->select();
            if(!empty($is_parent)){
                $this->error('修改失败！','index');
            }
            $res = Db::table('goods_specification')->where('id', $id)
                ->update(array(
                    'cid'  => $parent_id,
                    'name' => $name,
                    'create_time' => time(),
                    'status' => $status,
                ));
            if($res){
                $this->success('修改成功', 'index');
            }
        }else{//添加
            $id = input('id');
            $this->assign('id',$id);
            if($id){//如果存在代表查看某条记录
                $one_goods_specification = Db::table('goods_specification')->where(array('id'=>$id))->find();
                $this->assign('one_goods_specification',$one_goods_specification);
                //查询分类名称
                $one_category = Db::table('goods_category')->where(array('id'=>$one_goods_specification['cid']))->find();
                $this->assign('category_name',$one_category['name']);
            }
            $GoodsCategory = Db::table('goods_category')->where(array('status'=>1))->select();
            $this->assign('nodes',json_encode($GoodsCategory));
            return view('edit');
        }
        return view('edit');
    }

    //添加
    public function add(){
        if($_POST){ //提交表单
            $cid = $_POST['cid'];
            $name = $_POST['name'];
            $status = $_POST['status'];
            $data=array(
                'cid'=>$cid,
                'name'=>$name,
                'status'=>$status,
                'create_time'=>time()
            );
            $res = Db::table('goods_specification')->insert($data);
            if($res){
                $this->success('新增成功', 'index');
            }
        }else{
            $GoodsCategory = Db::table('goods_category')->where(array('status'=>1))->select();
            $this->assign('nodes',json_encode($GoodsCategory));
            return view('edit');
        }
    }
    //删除
    public function delete(){
        $id = input('id');
        $res = Db::table('goods_specification')->where(array('id'=>$id))->delete();
        if($res){
            $this->success('删除成功！', 'index');
        }else{
            $this->error('删除失败！','index');
        }
    }
}